package com.dd;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.chantel.tool.db.DBUtil;
import com.chantel.tool.db.PageData;
import com.chantel.tool.db.PageTool;

public class CaiGouShangPing {
	private static final Logger logger = Logger.getLogger(	CaiGouShangPing.class.getName());
	// get set 方法可以自动生成 页面 -- 右建 -- source - generate get set
	// ctrl+shif + f 自动整理代码
	// 采购商品ID
	private int caiGouShangPingId;

	// 采购表ID
	private int caiGouBiaoId;

	// 名称
	private String mingCheng;

	// 规格
	private String guiGe;

	// 数量
	private float shuLiang;

	// 单位
	private String danWei;

	// 历史价格
	private float liShiJiaGe;

	private ArrayList caiGouGongSiBaoJiaXinXis = null;

	public int getCaiGouBiaoId() {
		return caiGouBiaoId;
	}

	public void setCaiGouBiaoId(int caiGouBiaoId) {
		this.caiGouBiaoId = caiGouBiaoId;
	}

	public CaiGouShangPing() {
	}

	// 取历史价格取最低值，返回一条记录？？
	public static ArrayList GetShangPingsByMingCheng(String shangPingMing) {
		ArrayList arrList = new ArrayList();
		DBUtil du = DBUtil.getInstance();

		String sql = "select* from CaiGouShangPingTable where MingCheng like '%"
				+ shangPingMing + "%' order by LiShiJiaGe";
		List l = du.getRecords(sql);

		Iterator it = l.iterator();
		while (it.hasNext()) {
			Map m = (Map) it.next();

			int shangpingId = ((Integer) m.get("CaiGouShangPingId")).intValue();
			String danWei = (String) m.get("DanWei");
			String guiGe = (String) m.get("GuiGe");
			float liShiJiaGe = ((Double) m.get("LiShiJiaGe")).floatValue();
			String mingCheng = (String) m.get("MingCheng");
			float shuLiang = ((Double) m.get("ShuLiang")).floatValue();

			int caiGouBiaoId = ((Integer) m.get("CaiGouBiaoId")).intValue();

			CaiGouShangPing cgsp = new CaiGouShangPing(caiGouBiaoId, mingCheng,
					guiGe, shuLiang, danWei, liShiJiaGe);

			cgsp.setCaiGouShangPingId(shangpingId);

			arrList.add(cgsp);
		}
		return arrList;
	}

	public CaiGouShangPing(int caiGouBiaoId, String mingCheng, String guiGe,
			float shuLiang, String danWei, float liShiJiaGe) {
		super();

		this.caiGouBiaoId = caiGouBiaoId;
		// this.caiGouShangPingId = caiGouShangPingId;
		this.danWei = danWei;
		this.guiGe = guiGe;
		this.liShiJiaGe = liShiJiaGe;
		this.mingCheng = mingCheng;
		this.shuLiang = shuLiang;
	}

	public ArrayList GetCaiGouGongSiBaoJiaXinXis() {
		if (null != this.caiGouGongSiBaoJiaXinXis)
			this.caiGouGongSiBaoJiaXinXis.clear();

		this.caiGouGongSiBaoJiaXinXis = new ArrayList();

		List li = CaiGouGongSiBaoJiaXinXi
				.GetRecordsFromCaiGouShangPingId(this.caiGouShangPingId);
		Iterator it = li.iterator();

		while (it.hasNext()) {
			Map m = (Map) it.next();

			int id = ((BigDecimal) m.get("Id")).intValue();
			int gongSiId = ((BigDecimal) m.get("GongSiId")).intValue();
			String baoJia = (String) m.get("BaoJia");
			String shenHeJia = (String) m.get("ShenHeJia");

			CaiGouGongSiBaoJiaXinXi cgsp = new CaiGouGongSiBaoJiaXinXi(id,
					this.caiGouShangPingId, gongSiId, baoJia, shenHeJia);

			this.caiGouGongSiBaoJiaXinXis.add(cgsp);
		}

		return this.caiGouGongSiBaoJiaXinXis;
	}

	public static List GetRecordsFromCaiGouBiaoId(int caiGouBiaoId) {
		DBUtil du = DBUtil.getInstance();

		String sql = "select* from CaiGouShangPingTable where CaiGouBiaoId ="
				+ caiGouBiaoId;
		List l = du.getRecords(sql);

		return l;
	}

	// 执行数据删除
	public static boolean DeleteCaiGouShangPingFromCaiGouBiaoId(int caiGouBiaoId) {
		// TODO 执行数据删除
		DBUtil du = DBUtil.getInstance();

		String sql = "delete from CaiGouShangPingTable where CaiGouBiaoId = "
				+ caiGouBiaoId;

		boolean bool_res = du.deleteRecords(sql);

		return bool_res;

	}

	public void PutData(Map m) {
		m.put("CaiGouBiaoId", new Integer(this.caiGouBiaoId));
		m.put("MingCheng", this.mingCheng);
		m.put("GuiGe", this.guiGe);
		m.put("ShuLiang", new Float(this.shuLiang));
		m.put("DanWei", this.danWei);
		m.put("LiShiJiaGe", new Float(this.liShiJiaGe));
	}

	/**
	 * 执行数据插入 TOD0 error handling 为保证数据完整性，必须在一个事务中处理
	 */
	public boolean InsertShangPing() {
		// TODO 执行数据插入
		DBUtil du = DBUtil.getInstance();
		Map m = new HashMap();

		this.caiGouShangPingId = SequenceManager.nextID(SysGlobals.CGGS_ID);
		PutData(m);

		m.put("CaiGouShangPingId", new Integer(this.caiGouShangPingId));

		// do action
		boolean bool_res = du.insertRecord("CaiGouShangPingTable", m);
		return bool_res;
	}

	// 执行数据更新
	/**
	 * 为保证数据完整性，必须在一个事务中处理
	 */
	public boolean UpdateShangPing() {
		// TODO 执行数据更新
		DBUtil du = DBUtil.getInstance();
		Map m = new HashMap();

		PutData(m);

		String updateWhere = "CaiGouShangPingId=" + this.caiGouShangPingId;

		boolean bool_res = du.updateFieldsValue("CaiGouShangPingTable", m,
				updateWhere);

		return bool_res;
	}

	public ArrayList getCaiGouGongSiBaoJiaXinXis() {
		return caiGouGongSiBaoJiaXinXis;
	}

	public void setCaiGouGongSiBaoJiaXinXis(ArrayList caiGouGongSiBaoJiaXinXis) {
		this.caiGouGongSiBaoJiaXinXis = caiGouGongSiBaoJiaXinXis;
	}

	public int getCaiGouShangPingId() {
		return caiGouShangPingId;
	}

	public void setCaiGouShangPingId(int caiGouShangPingId) {
		this.caiGouShangPingId = caiGouShangPingId;
	}

	public String getDanWei() {
		return danWei;
	}

	public void setDanWei(String danWei) {
		this.danWei = danWei;
	}

	public float getLiShiJiaGe() {
		return liShiJiaGe;
	}

	public void setLiShiJiaGe(float liShiJiaGe) {
		this.liShiJiaGe = liShiJiaGe;
	}

	public String getMingCheng() {
		return mingCheng;
	}

	public void setMingCheng(String mingCheng) {
		this.mingCheng = mingCheng;
	}

	public float getShuLiang() {
		return shuLiang;
	}

	public void setShuLiang(float shuLiang) {
		this.shuLiang = shuLiang;
	}

	public String getGuiGe() {
		return guiGe;
	}

	public void setGuiGe(String guiGe) {
		this.guiGe = guiGe;
	}

	// 商品查询
	public static PageData getChaXunShangPing(String queryStr, int currentPage, int pageSize) {
		if ( currentPage < 0 )
			currentPage=0;
		if (pageSize<0 || pageSize>100)
			pageSize = 20;
		String sql = "select * from CaiGouShangPingTable where MingCheng like '%" + queryStr + "%'";
		logger.debug(sql);
		PageTool pgt = PageTool.getInstance();
		PageData pageData = pgt.getPageData(sql, currentPage, pageSize);
		return pageData;
	}

}
